// pages/myFamily/myFamily.js
const {
  getMyFamily,
  addMember
} = require('../../services/myFamilyReq')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    obj: {},
    volunteerAccount: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.handleGetFamily()
  },

  handleAddNow() {
    if (this.data.volunteerAccount === '') {
      wx.showToast({
        title: '请输入志愿者账号',
        icon: 'none'
      })
      return
    }

    addMember(this.data.volunteerAccount, res => {
      if (res.data.code !== 0) {
        wx.showToast({
          title: res.data.msg,
          icon: 'none'
        })
      } else {
        wx.showToast({
          title: '添加成功',
        })
        this.setData({
          volunteerAccount: ''
        })
        this.handleGetFamily()
      }
    })
  },
  accountChange(e) {
    this.setData({
      volunteerAccount: e.detail
    })
  },

  handleGetFamily() {
    wx.showLoading({
      title: '加载中',
    })
    const userInfo = wx.getStorageSync("volunteerInfo");
    console.log(userInfo)

    getMyFamily(res => {
      console.log(res)
      if (res.data.code !== 0) {
        wx.showToast({
          title: res.data.msg,
          icon: 'none'
        })
      } else {
        this.setData({
          obj: res.data.data
        })
        wx.hideLoading({
          success: (res) => {},
        })
      }
    })
  }
})
